<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户名" prop="username">
        <el-input
          v-model="queryParams.username"
          placeholder="请输入用户名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户昵称" prop="nickName">
        <el-input
          v-model="queryParams.nickName"
          placeholder="请输入用户昵称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label=" 真实名称" prop="realName">
        <el-input
          v-model="queryParams.realName"
          placeholder="请输入 真实名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择用户状态" clearable size="small">
          <el-option label="冻结" value="1" />
          <el-option label="正常" value="0" />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间" prop="registTime">
        <el-date-picker clearable size="small"
          v-model="queryParams.registTime"
          type="daterange"
          unlink-panels
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          placeholder="选择创建时间"
          value-format="yyyy-MM-dd">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['app:user:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="userList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="用户名" align="center" prop="username" />
      <el-table-column label="用户昵称" align="center" prop="nickName" />
      <el-table-column label=" 真实名称" align="center" prop="realName" />
      <el-table-column label="性别" align="center" prop="userSex" :formatter="sexFormat" />
      <el-table-column label="用户状态" align="center" prop="status" :formatter="statusFormat" />
      <el-table-column label="推荐人" align="center" prop="references" :formatter="referencesFormat" width="400" />
      <el-table-column label="余额" align="center" prop="wallet" />
      <el-table-column label="可用积分" align="center" prop="validScore" />
      <el-table-column label="创建时间" align="center" prop="registTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.registTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-share"
            @click="handlePreview(scope.row)"
            v-hasPermi="['app:user:edit']"
          >积分明细</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleEditTable(scope.row)"
            v-hasPermi="['app:user:edit']"
          >积分调整</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!--  积分明细  -->
    <el-dialog :title="preview.title" :visible.sync="preview.open" width="80%" top="5vh" append-to-body>
      <el-table :data="preview.data">
        <el-table-column label="订单时间" align="center" prop="changeTime" />
        <el-table-column label="类型" align="center" prop="changeType" :formatter="changeTypeFormat" />
        <el-table-column label="枚举" align="center" prop="changeScene" :formatter="changeSceneFormat" />
        <el-table-column label="积分" align="center" prop="changeValue" />
        <el-table-column label="备注" align="center" prop="remark" />
      </el-table>
      <pagination
        v-show="preview.total>0"
        :total="preview.total"
        :page.sync="previewParams.pageNum"
        :limit.sync="previewParams.pageSize"
        @pagination="getScoreInfoList"
      />
    </el-dialog>
    <!-- 积分调整  -->
    <el-dialog :title="changeScene" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="调整类型" prop="type">
              <el-radio v-model="form.type" label="1">增加</el-radio>
              <el-radio v-model="form.type" label="2">减少</el-radio>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="变动值" prop="score">
              <el-input-number v-model="form.score" :min="1" value="1" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="调整备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" :rows="2" placeholder="请输入调整备注" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listUser, exportUser, scoreInfo, changeScore } from "@/api/app/user";

export default {
  name: "User",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 会员表格数据
      userList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      changeScene: null,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        username: null,
        nickName: null,
        realName: null,
        status: null,
        registTime: null,
        startTime: null,
        endTime: null
      },
      // 预览参数
      previewParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null
      },
      preview: {
        open: false,
        title: "积分明细",
        data: {},
        username: "",
        total: 0
      },
      form: {
        score: 0,
        remark: null,
        userId: null,
        type: "1"
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询会员列表 */
    getList() {
      this.loading = true;
      if (this.queryParams.registTime) {
        this.queryParams.startTime = this.queryParams.registTime[0]
        this.queryParams.endTime = this.queryParams.registTime[1]

        this.queryParams.registTime = null
      }
      listUser(this.queryParams).then(response => {
        this.userList = response.rows;
        this.total = response.total;
        this.loading = false;

        if (this.queryParams.startTime)
          this.queryParams.registTime = [
            this.queryParams.startTime,
            this.queryParams.endTime
          ]
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.userId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有会员数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportUser(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    },
    sexFormat(row) {
      return row.sex === 1 ? "男" : "女"
    },
    statusFormat(row) {
      return row.status === 1 ? '冻结' : '正常'
    },
    referencesFormat(row) {
      if (!row.auUsername && !row.auRealName && !row.auNickName) return "无"

      return row.auUsername + " - " + row.auRealName + " - " + row.auNickName
    },
    handlePreview(row) {
      this.previewParams.userId = row.userId
      this.previewParams.pageNum = 1;
      this.getScoreInfoList();
    },
    getScoreInfoList() {
      scoreInfo(this.previewParams).then(response => {
        this.preview.data = response.rows;
        this.preview.open = true;
        this.preview.total = response.total

      })
    },

    changeTypeFormat(row) {
      return row.changeType === 1 ? '获取' : '消耗'
    },

    changeSceneFormat(row) {
      switch (row.changeScene) {
        case 1:
          return '线下消费'
        case 2:
          return '积分兑换'
        case 3:
          return '订单退款'
        case 4:
          return '商城'
        case 5:
          return '手动调整'
        default:
          return '未知状态'
      }
    },

    handleEditTable(row) {
      const tableId = row.username;
      this.changeScene = tableId + " - 积分调整"

      this.open = true;
      this.reset();
      this.form.userId = row.userId;
    },

    reset() {
      this.form = {
        userId: null,
        score: 1,
        remark: null,
        type: "1"
      }
      this.resetForm("form");
    },

    /** 提交按钮 */
    submitForm: function() {
      console.log(this.form)
      changeScore(this.form).then(response => {
        this.msgSuccess("修改成功");
        this.open = false;
        this.getList();
      });
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
  }
};
</script>
